﻿var countdown = 90;
function sendVerifyCode() {
    var obj = $("#btnSend");
    var customCode = $("#CustmCode").val();
    var username = $("#Username").val();
    $.post("/Admin/Index/SendCode", { customCode: customCode, username: username}, function (result) {
        if (result.success) {
            swal({
                title: "操作提示",
                text: result.msg,
                type: "success",
                closeOnConfirm: false,
                confirmButtonText: "确定",
                cancelButtonText: "取消"
            });
        }
        else {
            swal({
                title: "操作提示",
                text: result.msg,
                type: "warning",
                closeOnConfirm: false,
                confirmButtonText: "确定",
                cancelButtonText: "取消"
            });
        }
    });
    settime(obj);
}
function settime(obj) { //发送验证码倒计时
    if (countdown == 0) {
        obj.attr('disabled', false);
        obj.val("获取短信验证码");
        countdown = 60;
        return;
    } else {
        obj.attr('disabled', true);
        obj.val("重新发送(" + countdown + "s)");
        countdown--;
    }
    setTimeout(function () {
        settime(obj)
    }, 1000)
}

$(function () {
    $("#secImg").click(function () {
        ///注意后面的flag是必须的，如果不添加会导致部分浏览器不能刷新验证码
        $("#secImg").attr("src", "/Admin/Index/GetSecurityCode?flag=" + Math.random());
    });

    $("#Username").focus();

    $('#login').validate({
        errorElement: 'span',
        errorClass: 'help-inline',
        focusInvalid: true,
        rules: {
            CustmCode: "required",
            Username: "required",
            Password: "required",
            VerifyCode: "required",
            SecurityCode: "required"
        },
        messages: {
            CustmCode:"请输入商户号",
            Username: "请输入用户名",
            Password: "请输入密码",
            VerifyCode:"请输入手机验证码",
            SecurityCode: "请输入验证码"
        },
        highlight: function (e) {
            $(e).closest('.form-group').removeClass('info').addClass('has-error');
        },
        success: function (e) {
            $(e).closest('.form-group').removeClass('has-error').addClass('info');
            $(e).remove();
        },
        submitHandler: function (form) {
            $.ajax({
                url: "/Admin/Index/Login",
                type: "POST",
                data: $('#login').serialize(),
                beforeSend: function () {
                    $('#btnLogin').text("Login...");
                },
                success: function (response) {
                    if (response.success) {
                        window.location.href = response.url;
                    } else {
                        swal({
                            title: "操作提示",
                            text: response.msg,
                            type: "error",
                            closeOnConfirm: true,
                            confirmButtonText: "确定",
                            cancelButtonText: "取消"
                        });
                    };
                },
                complete: function () {
                    $('#btnLogin').text("Login");
                }
            });
        }
    });
});
